package com.zyz.test;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class GetQrCodeImage {

	//获取二维码接口连接地址
	private static String url = "http://127.0.0.1:8080/cssjzsgj/application/getQrCodeImage.action";
	
	public static void main(String[] args) {
		getQrCodeImage();
	}
	
	/**
	 * 获取二维码图片的方法
	 */
	public static void getQrCodeImage(){
		
		try {
			 HttpClient httpClient = new DefaultHttpClient();
			 //获取链接
			 HttpPost httpPost = new HttpPost(url);
			 //Content-Type是application/x-www-form-urlencoded
			 httpPost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
			 List<NameValuePair> params = new ArrayList<NameValuePair>();
			 // userNme表示用户名,systemName表示系统名称,qrCodeContent表示二维码内容,这三项为必填项。
			 String userName ="管理员";
			 String systemName ="数据与展示工具系统";
			 String qrCodeContent = "测试获取二维码图片的接口";
			 params.add(new BasicNameValuePair("userName",userName));
			 params.add(new BasicNameValuePair("systemName",systemName));
			 params.add(new BasicNameValuePair("qrCodeContent",qrCodeContent));
			 httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
			 HttpResponse response = null;  
			 response = httpClient.execute(httpPost);
			 InputStream inputStream = response.getEntity().getContent();
			 //path是二维码图片输出的路径,该路径必须存在
			 String path = "D:\\Test\\qrCode.png";
			 FileOutputStream fos = new FileOutputStream(path);
			 byte[] qrcodeByte = new byte[1024];
			 int length =0;
			 while((length=inputStream.read(qrcodeByte))>0){
			    fos.write(qrcodeByte,0,length);
			 }
			 inputStream.close();
			 fos.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

}
